#ifndef __DEVSERVICE_H
#define __DEVSERVICE_H


#include "common.h"

#define WATER_PH_HANDLE()                        \
    g_WATER_PH = (u16)(-5.7541 * volt + 16.654); \
    if (g_WATER_PH <= 0)                         \
    {                                            \
        g_WATER_PH = 0;                          \
    }                                            \
    else if (g_WATER_PH >= 14)                   \
    {                                            \
        g_WATER_PH = 14;                         \
    }                                            \
    else                                         \
    {                                            \
    }

#define WATER_TDS_HANDLE()                                                          \
    float TDSkValue = 1.5;                                                          \
    float TDS_CompeCoe = 1.0 + 0.02 * (g_WATER_TMPR / 10 - 25.0);                   \
    float TDS_Volatge = volt / TDS_CompeCoe;                                        \
    float TDS_value = (133.42 * TDS_Volatge * TDS_Volatge * TDS_Volatge -           \
                       255.86 * TDS_Volatge * TDS_Volatge + 857.39 * TDS_Volatge) * \
                      0.5 * TDSkValue;                                              \
    g_WATER_TDS = (u16)TDS_value;                                                   \
    if ((g_WATER_TDS <= 0))                                                         \
    {                                                                               \
        g_WATER_TDS = 0;                                                            \
    }                                                                               \
    if ((g_WATER_TDS > 1500))                                                       \
    {                                                                               \
        g_WATER_TDS = 1500;                                                         \
    }

#define WATER_NTU_HANDLE()                                              \
    float TU_calibration = -0.0192 * ((g_WATER_TMPR / 10) - 25) + volt; \
    g_WATER_NTU = (u16)(-865.68 * TU_calibration + 2500);               \
    if (g_WATER_NTU <= 0)                                               \
    {                                                                   \
        g_WATER_NTU = 0;                                                \
    }                                                                   \
    if (g_WATER_NTU >= 3000)                                            \
    {                                                                   \
        g_WATER_NTU = 3000;                                             \
    }

#define WATER_TMPR_HANDLE()                       \
    g_WATER_TMPR = (1.5 - volt) * 1000 / 3 + 250; \
    if (g_WATER_TMPR > 1000)                      \
        g_WATER_TMPR = 1000;                      \
    if (g_WATER_TMPR < -400)                      \
        g_WATER_TMPR = -400;

#define SMOKE_HANDLE()\
    g_SMOKE = (100 - (3.3 - volt) * 100 / 3.3) * 5;
    
#define SHAKE_HANDLE()\
    g_SHAKE = (3.3 - volt) * 100 / 3.3;

#define RAINMETER_HANDLE()\
    g_RAINMETER = (3.3 - volt) * 100 / 3.3;

#define TEMINALTMPR_HANDLE()                       \
    g_TEMINALTMPR = (1.5 - volt) * 1000 / 3 + 250; \
    if (g_TEMINALTMPR > 1000)                      \
        g_TEMINALTMPR = 1000;                      \
    if (g_TEMINALTMPR < -400)                      \
        g_TEMINALTMPR = -400;
#define ILLUMATION_HANDLE() \
    g_ILLUMATION = (3.3 - volt) * 100 / 3.3;

#define MOISTURE_HANDLE() \
    g_MOISTURE = volt * 100 / 3.3;

#define PRESS1_HANDLE() \
    float ress = 1.0 / ( (10*volt) / (3.3 - volt) );\
    g_PRESS1 = (s16)( 741710 * ress - 840.97);\
	if(g_PRESS1 < 0) g_PRESS1 = 0;\
	if(g_PRESS1 > 12000) g_PRESS1 = 12000;
	
#define PRESS2_HANDLE() \
    float ress = 1.0 / ( (10*volt) / (3.3 - volt) );\
    g_PRESS2 = (s16)( 741710 * ress - 840.97);\
	if(g_PRESS2 < 0) g_PRESS2 = 0;\
	if(g_PRESS2 > 12000) g_PRESS2 = 12000;

#define DEFINE_ADC_DEV(name)                           \
    BOOL_E name##_Init(void)                           \
    {                                                  \
        ADC_Init(CFG_##name##_CH);                     \
        return TRUE;                                   \
    }                                                  \
    BOOL_E name##_Read(void)                           \
    {                                                  \
        u32 tempVal = 0;                               \
        tempVal = Get_Adc_Average(CFG_##name##_CH, 5); \
        float volt = 3.3 * tempVal / 4096;             \
        name##_HANDLE();                               \
        return TRUE;                                   \
    }

#define DECLARE_ADC_DEV(name)   \
    extern BOOL_E name##_Init(void);  \
    extern BOOL_E name##_Read(void);

#define DEFINE_IO_DEV(type, name)                                   \
    BOOL_E name##_Init(void)                                     \
    {                                                            \
        type##_init(name##_PIN, name##_PORT);                     \
        return TRUE;                                             \
    }                                                            \
    BOOL_E name##_Read(void)                                     \
    {                                                            \
        g_##name##_STA = type##_read(name##_PIN, name##_PORT); \
        return TRUE;                                             \
    }                                                            \
    BOOL_E name##_Write(void *pMeter)                            \
    {                                                            \
        if (pMeter == NULL)                                      \
        {                                                        \
            if (g_##name##_CTRL == 1)                            \
            {                                                    \
                type##_on(name##_PIN, name##_PORT);            \
            }                                                    \
            else                                                 \
            {                                                    \
                type##_off(name##_PIN, name##_PORT);             \
            }                                                    \
        }                                                        \
        else                                                     \
        {                                                        \
            if (*(u16 *)pMeter == 1)                             \
            {                                                    \
                type##_on(name##_PIN, name##_PORT);            \
            }                                                    \
            else                                                 \
            {                                                    \
                type##_off(name##_PIN, name##_PORT);             \
            }                                                    \
        }                                                        \
        return TRUE;                                             \
    }

#define DECLARE_IO_DEV(name)      \
    extern BOOL_E name##_Init(void); \
    extern BOOL_E name##_Read(void); \
    extern BOOL_E name##_Write(void *pMeter);


DECLARE_IO_DEV(RELAY1);
DECLARE_IO_DEV(RELAY2);
DECLARE_IO_DEV(RELAY3);   

DECLARE_IO_DEV(BEEP);

DECLARE_IO_DEV(HEART_LED);
DECLARE_IO_DEV(LED1);
DECLARE_IO_DEV(LED2);
DECLARE_IO_DEV(LED3);
#if CFG_USE_SMOKE == 1	
DECLARE_ADC_DEV(SMOKE);
#endif
DECLARE_ADC_DEV(SHAKE);
DECLARE_ADC_DEV(RAINMETER);
DECLARE_ADC_DEV(TEMINALTMPR);

DECLARE_ADC_DEV(WATER_PH);
DECLARE_ADC_DEV(WATER_TDS);
DECLARE_ADC_DEV(WATER_NTU);
DECLARE_ADC_DEV(WATER_TMPR);

DECLARE_ADC_DEV(ILLUMATION);
DECLARE_ADC_DEV(MOISTURE);

DECLARE_ADC_DEV(PRESS1);
DECLARE_ADC_DEV(PRESS2);

extern BOOL_E GPS_Init(void);
extern BOOL_E GPS_Read(void);

extern BOOL_E DHT11_Read(void);

extern void AllDevInit(void);
extern BOOL_E LORA_Init(void);
extern BOOL_E WIFI_Init(void);

extern void Sensor_Read_Handle(void *pvParameters);
extern void Lora_Rev_Handle(void *pvParameters);
extern void Lora_Send_Handle(void *pvParameters);
extern void Dev_Ctrl_Excute(void *pvParameters);

extern void OLED_Show(void *pvParameters);
extern void Dev_Ctrl_Cmd(u16 onoff);


extern PROJ_DEV_S g_astProjAllDev[];

extern u16 get_AllDevNum(void);

#if CFG_USE_MOTOR == 1
extern BOOL_E MOTOR_Init(void);
extern BOOL_E MOTOR_Write(void* pvParameters);
#endif

#endif
